From 72023edbdd7784120d1f9c7e2ef2688cf398f22d Mon Sep 17 00:00:00 2001 From: Noah Meyerhans Date: Wed, 2 Sep 2026 12:17:57 -0400 Subject: [PATCH] [PATCH] Correctly handle signed 32-bit time_t types Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1124541 Forwarded: no dovecot handles 32-bit time_t in a couple of different ways, but neither quite works currently. Setting TIME_T_MAX_BITS to 31 isn't correctly handled in places where time_t values are constructed, for example in io_loop_get_wait_time(). Similarly, setting TIME_T_MAX_BITS = 32 and defining TIME_T_SIGNED is not correctly handled by tm_is_too_large(). This change fixes tm_is_too_large() to set max_time to the correct maximum date representable by a signed 32-bit time_t. Closes: #1124541 Gbp-Pq: Name Correctly_handle_signed_32-bit_time_t_types.patch --- src/lib-imap/test-imap-date.c | 16 ++++++---------- src/lib/time-util.c | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/lib-imap/test-imap-date.c b/src/lib-imap/test-imap-date.c index 22113e0..b5d0024 100644 --- a/src/lib-imap/test-imap-date.c +++ b/src/lib-imap/test-imap-date.c @@ -15,7 +15,7 @@ static void test_imap_date(void) } tests[] = { { "01-Jan-1970", 0 }, { "19-Jan-2038", 2147472000 }, -#if TIME_T_MAX_BITS >= 32 +#if TIME_T_MAX_BITS > 32 { "07-Feb-2106", 4294944000 }, #endif #if TIME_T_MAX_BITS >= 37 @@ -25,12 +25,9 @@ static void test_imap_date(void) { "31-Dec-9999", 253402214400LL }, #endif /* conversions to maximum values */ -#if TIME_T_MAX_BITS == 31 +#if TIME_T_MAX_BITS <= 32 { "20-Jan-2038", 2147483647 }, { "31-Dec-9999", 2147483647 }, -#elif TIME_T_MAX_BITS == 32 - { "08-Feb-2106", 4294967295 }, - { "31-Dec-9999", 4294967295 }, #endif }; const char *invalid_tests[] = { @@ -59,8 +56,10 @@ static void test_imap_datetime(void) } tests[] = { { "01-Jan-1970 00:00:00 +0000", 0, 0 }, { "19-Jan-2038 03:14:07 +0000", 2147483647, 0 }, +#if TIME_T_MAX_BITS > 32 { "19-Jan-2038 05:14:07 +0200", 2147483647, 2*60 }, -#if TIME_T_MAX_BITS >= 32 +#endif +#if TIME_T_MAX_BITS > 32 { "07-Feb-2106 06:28:15 +0000", 4294967295, 0 }, #endif #if TIME_T_MAX_BITS >= 37 @@ -71,12 +70,9 @@ static void test_imap_datetime(void) { "31-Dec-9999 23:59:59 -2359", 253402300799LL + 23*60*60 + 59*60, -23*60 - 59 }, #endif /* conversions to maximum values */ -#if TIME_T_MAX_BITS == 31 +#if TIME_T_MAX_BITS <= 32 { "19-Jan-2038 03:14:08 +0000", 2147483647, 0 }, { "31-Dec-9999 23:59:59 -2359", 2147483647, -23*60 - 59 }, -#elif TIME_T_MAX_BITS == 32 - { "07-Feb-2106 06:28:16 +0000", 4294967295, 0 }, - { "31-Dec-9999 23:59:59 -2359", 4294967295, -23*60 - 59 }, #endif }; const char *invalid_tests[] = { diff --git a/src/lib/time-util.c b/src/lib/time-util.c index 4f89591..b035375 100644 --- a/src/lib/time-util.c +++ b/src/lib/time-util.c @@ -166,7 +166,7 @@ time_t time_max_safe_value(void) #else /* compute in uint64_t: with a 32-bit signed time_t TIME_T_MAX_BITS is 31 and (time_t)1 << 31 would overflow */ - return (time_t)(((uint64_t)1 << TIME_T_MAX_BITS) - 1); + return (time_t)(((uint64_t)1 << (TIME_T_MAX_BITS - 1)) - 1); #endif } -- 2.30.2